package com.yungu.swift.order.helper.chain;

import com.yungu.swift.lbs.model.DriverPoint;
import com.yungu.swift.order.helper.model.AssignScoreParam;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 司机距离处理器
 * @author : cuixiuyin
 * @date : 2019/7/10
 */
public class DriverDistanceHandler extends AssignScoreHandler {

    /**
     * 派单距离
     */
    private Integer maxDistance;
    /**
     * 司机距离
     */
    private List<DriverPoint> filterDriver;

    public DriverDistanceHandler(Integer maxDistance, List<DriverPoint> filterDriver) {
        this.maxDistance = maxDistance;
        this.filterDriver = filterDriver;
    }

    @Override
    public void calculation(List<AssignScoreParam> scoreParams) {
        Map<String, Double> distanceMap = filterDriver.stream().collect(Collectors.toMap(DriverPoint::getDriverUuid,
                DriverPoint::getDistance, (oldValue, newValue) -> newValue));
        scoreParams.forEach(scoreParam -> {
            Double driverDistance = distanceMap.get(scoreParam.getDriverUuid());
            scoreParam.setScore(scoreParam.getScore() + score(driverDistance));
        });
        if (nextHandler != null) {
            nextHandler.calculation(scoreParams);
        }
    }

    /**
     * 计算司机离派单点距离分数
     * --     1.4  0 < x 1/3
     * f(x) = 1.2  1/3 < x < 2/3
     * --     1.0  3/2 < x < 1
     * @param driverDistance
     * @return
     */
    private Double score(Double driverDistance) {
        if (driverDistance < maxDistance / 3){
            return 1.4;
        }
        if (driverDistance < maxDistance * 2 / 3 ){
            return 1.2;
        }
        return 1.0;
    }
}
